using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace Brick
{
	public class WaitTimerManager
	{
		private class TaskBehaviour : MonoBehaviour
		{
		}

		private static TaskBehaviour m_Task;

		static WaitTimerManager()
		{
			GameObject gameObject = new GameObject("#WaitTimerManager#");
			Object.DontDestroyOnLoad(gameObject);
			m_Task = gameObject.AddComponent<TaskBehaviour>();
		}

		public static Coroutine WaitTime(float time, UnityAction callback)
		{
			return m_Task.StartCoroutine(Coroutine(time, callback));
		}

		public static void CancelWait(ref Coroutine coroutine)
		{
			if (coroutine != null)
			{
				m_Task.StopCoroutine(coroutine);
				coroutine = null;
			}
		}

		private static IEnumerator Coroutine(float time, UnityAction callback)
		{
			yield return new WaitForSeconds(time);
			callback?.Invoke();
		}
	}
}

